package com.bocloud.cmp.server.controller.resource;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.resource.SafetyDevice;
import com.bocloud.cmp.interfaces.resource.SafetyDeviceService;
import com.bocloud.cmp.model.SafetyDeviceBean;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.RequestUser;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.ResultTools;

/**
 * 物理安全设备相关接口
 * 
 * @author wei wei
 * @version 1.0
 * @since 2017.02.23
 *
 */
@RestController
@RequestMapping("/device/safety")
public class SafetyDeviceController {

	@Autowired
	private SafetyDeviceService safetyDeviceService;

	/**
	 * 列表
	 * 
	 * @param page
	 *            当前页码
	 * @param rows
	 *            页面数据大小
	 * @param params
	 *            查询参数，例如：[{"param":{"name":"aaa","password":"1245"},"sign":
	 *            "EQ|UEQ"},{"param":{"name":"aaa","password":"1245"},"sign":
	 *            "EQ|UEQ"}]
	 * @param sorter
	 *            排序参数，例如：{"name":0|1,"password":0|1},0表示增序，1表示降序
	 * @param simple
	 *            简单查询标记，只有true和false,为false时返回服务器的详细信息，为true时只返回id和name值。
	 * @return 数据结果集
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST })
	public BsmResult list(@RequestParam(value = Common.PAGE, required = false) Integer page,
			@RequestParam(value = Common.ROWS, required = false) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.SIMPLE, required = false) Boolean simple,
			@Value(Common.REQ_USER) RequestUser user) {
		@SuppressWarnings("unchecked")
		Map<String, String> sorterMap = JSONObject.parseObject(sorter, HashMap.class);
		return safetyDeviceService.list(page, rows, JSONObject.parseArray(params, Param.class), sorterMap, simple);
	}

	/**
	 * 添加
	 * 
	 * @param params
	 *            属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @param user
	 *            操作者
	 * @return 添加结果
	 */
	@RequestMapping(value = "/create", method = { RequestMethod.POST })
	public BsmResult create(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_USER) RequestUser user) {
		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {
			SafetyDevice device = JSONObject.parseObject(object.toJSONString(), SafetyDevice.class);
			return safetyDeviceService.create(device, user.getId());
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 修改
	 * 
	 * @param params
	 *            属性信息的json字符串，在数据转换时会发生异常，对外抛出400异常【Bad Request】
	 * @param id
	 *            ID
	 * @param user
	 *            操作者
	 * @return
	 */
	@RequestMapping(value = "/{id}/modify", method = { RequestMethod.POST })
	public BsmResult modify(@RequestParam(value = Common.PARAMS, required = true) String params,
			@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
		JSONObject object = JSONTools.isJSONObj(params);
		if (null != object) {
			SafetyDeviceBean bean = JSONObject.parseObject(object.toJSONString(), SafetyDeviceBean.class);
			return safetyDeviceService.modify(bean, user.getId());
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 移除
	 * 
	 * @param id
	 *            ID
	 * @param user
	 *            操作者
	 * @return 操作结果
	 */
	@RequestMapping(value = "/{id}/remove", method = { RequestMethod.DELETE })
	public BsmResult remove(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
		return safetyDeviceService.remove(id, user.getId());
	}

	/**
	 * 详细信息
	 * 
	 * @param id
	 *            ID
	 * @param user
	 *            操作者
	 * @return 操作结果
	 */
	@RequestMapping(value = "/{id}/detail", method = { RequestMethod.GET })
	public BsmResult detail(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
		return safetyDeviceService.detail(id);
	}

	/**
	 * 启动
	 * 
	 * @param id
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/{id}/start", method = { RequestMethod.POST })
	public BsmResult start(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
		return safetyDeviceService.start(id);
	}

	/**
	 * 安全设备批量启动
	 * 
	 * @param params
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/batchStart", method = { RequestMethod.POST })
	public BsmResult batchtStart(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_USER) RequestUser user) {
		return safetyDeviceService.batchStart(params, user.getId());
	}

	/**
	 * 设备批量停止
	 * 
	 * @param params
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/batchStop", method = { RequestMethod.POST })
	public BsmResult batchStop(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_USER) RequestUser user) {
		return safetyDeviceService.batchStop(params, user.getId());
	}

	/**
	 * 安全设备批量删除
	 * 
	 * @param params
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/batchRemove", method = { RequestMethod.POST })
	public BsmResult batchRemove(@RequestParam(value = Common.PARAMS, required = true) String params,
			@Value(Common.REQ_USER) RequestUser user) {
		return safetyDeviceService.batchRemove(params, user.getId());
	}

	/**
	 * 停止
	 * 
	 * @param id
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/{id}/stop", method = { RequestMethod.POST })
	public BsmResult stop(@PathVariable(value = Common.ID) Long id, @Value(Common.REQ_USER) RequestUser user) {
		return safetyDeviceService.stop(id);
	}

}
